import docx,re
from _add_hyperlink import add_hyperlink
# def add_hyperlink(paragraph, url, text):

def trans_link(docxfile):
    document = docx.Document(docxfile)
    for paragraph in document.paragraphs:
        lines = paragraph.text.split('\n')
        for line in lines:
            if "https" in line:
                url=re.findall(r'https.*',line)# re.findall是列表
#                 print(url[0])
                
#                 paragraph = document.add_paragraph()
#                 run = paragraph.add_run()
#                 run.text = "百度" + paragraph.text失败
#                 paragraph = document.add_paragraph()# 先追加空行，再加入超链接【在末尾？？？】
                add_hyperlink(paragraph,url[0],'链接')# 在找到的line后一段添加【必须有空行，否则都在末尾】
    document.save(docxfile)
# 方法1
# docxfile="C:\\Users\\King\\Desktop\\1.docx"
# 方法2失败
# docxfile=input("请输入要转为超链接的docx文件地址：").replace("\\",'/')
# print(docxfile)
# 方法3
# docxfile="C:\\Users\\King\\Desktop\\sci\\"+input("请输入要转为超链接的【docx文件名，无后缀】：")+".docx"
# trans_link('C:/Users/King/Desktop/1.docx')#成功
# trans_link(docxfile)#失败【Permission denied】
                